<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Manage;
use App\Validate\ManageInfoValidate;
use Illuminate\Http\Request;

class ManageController extends CommonController
{
    public $manageModelObj = null;
    protected $validateObj = null;

    public function __construct()
    {
        parent::__construct();

        $this->manageModelObj = new Manage();
        $this->validateObj = new ManageInfoValidate();
    }

    /**
     * 管理员列表
     * @param keyword 关键字搜索
     * @param page 页数    默认第一页
     * @param limit 限制条数  默认 10
     * @param start_time 开始时间
     * @param end_time 结束时间
     */
    public function manageList()
    {
        $keyword = $this->request->input('keyword', '');
        $start_time = $this->request->input('start_time', '');
        $end_time = $this->request->input('end_time', '');
        $limit = $this->request->input('limit', 10);
        $page = $this->request->input('page', 1);
        $res = $this->manageModelObj->select(['id', 'account', 'username', 'tel', 'create_time', 'end_login_time', 'end_login_ip'])
            ->where(function ($query) use ($start_time, $end_time) {
                if (!empty($start_time) && !empty($end_time)) {
                    $query->whereBetween('create_time', [$start_time, $end_time]);
                }
            })
            ->where(function ($query) use ($keyword) {
                if ($keyword) {
                    $query->orWhere('account', 'like', '%' . $keyword . '%')->orWhere('username', 'like', '%' . $keyword . '%');
                }
            })
            ->where('is_del', 1)
            ->orderByDesc('id')
            ->paginate($limit)
            ->toArray();

        if ($res['data']) {
            $res = $this->disPageData($res);
            //增加序号
            $res['data'] = $this->addSerialNumber($res['data'], $page, $limit);

            return $this->returnApi(200, '获取成功', true, $res);
        }

        return $this->returnApi(203, '暂无数据');
    }

    /**
     * 管理员添加
     * @param account 账号，不能存在中文  只能是中文或数字    必填
     * @param username 姓名至少2位数         必填
     * @param password 密码 中文加英文   6-20 位之间     必填
     * @param confirm_password 密码 中文加英文   6-20 位之间    必填
     * @param tel 电话号码
     */
    public function manageAdd()
    {
        //增加验证场景进行验证
        if (!$this->validateObj->scene('manage_add')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validateObj->getError());
        }

        //验证密码
        $validate_password = validate_password($this->request->password);
        if ($validate_password !== true) {
            return $this->returnApi(201, $validate_password);
        }

        //判断电话号码是否符合要求
        if (!empty($this->request->tel) && !verifyTel($this->request->tel)) {
            return $this->returnApi(201,  '电话号码格式不正确');
        }

        $is_exists = $this->manageModelObj->manageAccountIsExists($this->request->account);
        if ($is_exists) {
            return $this->returnApi(202, '此名称已存在，请重新添加');
        }

        $res = $this->manageModelObj->add($this->request);

        if ($res) {
            return $this->returnApi(200, '添加成功', true);
        }
        return $this->returnApi(202, '添加失败');
    }

    /**
     * 管理员修改
     * @param id 管理员id
     * @param username 姓名至少2位数   必填
     * @param tel 电话号码
     */

    public function manageChange()
    {
        //增加验证场景进行验证
        if (!$this->validateObj->scene('manage_change')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validateObj->getError());
        }


        //判断电话号码是否符合要求
        if (!empty($this->request->tel) && !verifyTel($this->request->tel)) {
            return $this->returnApi(201,  '电话号码格式不正确');
        }

        $is_exists = $this->manageModelObj->manageAccountIsExists($this->request->account, $this->request->id);
        if ($is_exists) {
            return $this->returnApi(202, '此名称已存在，请重新修改');
        }

        $res = $this->manageModelObj->change($this->request);

        if ($res) {
            return $this->returnApi(200, '修改成功', true);
        }

        return $this->returnApi(202, '修改失败');
    }

    /**
     * 管理员删除
     * @param id  管理员id  必选
     */

    public function manageDel()
    {
        //增加验证场景进行验证
        if (!$this->validateObj->scene('manage_del')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validateObj->getError());
        }
        $res = $this->manageModelObj->where('is_del', 1)->find($this->request->id);
        if (empty($res)) {
            return $this->returnApi(202, '删除失败');
        }
        $res->is_del = 2;
        $result = $res->save();

        if ($result) {
            return $this->returnApi(200, '删除成功', true);
        }

        return $this->returnApi(202, '删除失败');
    }

    /**
     * 管理员修改密码
     * @param id  管理员id  必选
     * @param password 密码 中文加英文   6-20 位之间     必填
     * @param confirm_password 密码 中文加英文   6-20 位之间    必填
     */
    public function manageChangePwd()
    {
        //增加验证场景进行验证
        if (!$this->validateObj->scene('manage_change_pwd')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validateObj->getError());
        }

        //验证密码
        $validate_password = validate_password($this->request->password);
        if ($validate_password !== true) {
            return $this->returnApi(201, $validate_password);
        }


        $res = $this->manageModelObj->where('is_del', 1)->find($this->request->id);
        if (empty($res)) {
            return $this->returnApi(202, '修改失败');
        }
        $res->password = md5($this->request->password);
        $result = $res->save();

        if ($result) {
            return $this->returnApi(200, '修改成功', true);
        }

        return $this->returnApi(202, '修改失败');
    }


    /**
     * 管理员修改密码
     * @param token  管理员token  必选
     * @param password 密码 中文加英文   6-20 位之间     必填
     * @param confirm_password 密码 中文加英文   6-20 位之间    必填
     */
    public function manageChangeSelfPwd()
    {

        //增加验证场景进行验证
        if (!$this->validateObj->scene('manage_change_self_pwd')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validateObj->getError());
        }
        $res = $this->manageModelObj->where('is_del', 1)->find($this->request->manage_id);
        if (empty($res)) {
            return $this->returnApi(202, '修改失败');
        }
        $res->password = md5($this->request->password);
        $result = $res->save();

        if ($result) {
            return $this->returnApi(200, '修改成功', true);
        }

        return $this->returnApi(202, '修改失败');
    }
}
